//Version 0.1
/*
Index
ControlID
MessageID
Mandatory
MMessage
MinLength
MaxLength
LMessage
Regex
RMessage
*/

function Validation(){
	this.Data = [];
	this.MessageHelper = [];
	this._customizeValidation = [];
	this.Initial = function(){
		if(this.Data.length>0){
			for(var i=0;i<this.Data.length;i++){
				var obj = document.getElementById(this.Data[i].ControlID);		
				if(obj.tagName.toUpperCase() == "INPUT" && obj.attributes["type"].value=="text"){
					obj.onblur = function(){
						getValidationInstance()._controlValidate(this);
					}
				}
			}
		}
	}
	this._controlValidate = function(obj){
		var data = getValidationInstance()._fetchData(obj.id);
		var _sType = data.DisplayType;
		var ctrlmsg = new ControlMessage(data.ControlID,"");
		if(obj.value == "" && (new Boolean(data.Mandatory))){			
			ctrlmsg.Message = data.MMessage;		
		}else if(obj.value.length < parseInt(data.MinLength) || obj.value.length > parseInt(data.MaxLength)){
			ctrlmsg.Message = data.LMessage;
		}else{
			var _reg = new RegExp(data.Regex);
			if(!_reg.test(obj.value)){
				ctrlmsg.Message = data.RMessage;
			}
		}		
		
		if(ctrlmsg.Message != ""){
			getValidationInstance()._AddMessage(data.MessageID, ctrlmsg);
			getValidationInstance().Display();
			return false;
		}
		else{
			getValidationInstance()._AddMessage(data.MessageID,obj);
			getValidationInstance().Display();
			return true;
		}
	}
	
	this._fetchData = function(objId){
		for(var i=0;i<this.Data.length;i++){
			if(this.Data[i].ControlID==objId)
				return this.Data[i];
		}
		return [];	
	}
	
	this.Validate = function(){
		var result = true;
		//Control Validation
		for(var i=0;i<this.Data.length;i++){
			var obj = document.getElementById(this.Data[i].ControlID);
			result = result & this._controlValidate(obj);
		}
		//Customize Validation
		for(var i=0;i<this._customizeValidation.length;i++){
		    if(this._customizeValidation[i] != null && typeof(this._customizeValidation[i].Validate) == "function")
		        var _result = true;
		        try{
		            _result = this._customizeValidation[i].Validate(result);
		            if(_result == false){
		                var ctrlMsg = new ControlMessage(this._customizeValidation[i].UniqueID, this._customizeValidation[i].Message);
		                getValidationInstance()._AddMessage(this._customizeValidation[i].MessageID, ctrlMsg);
		                getValidationInstance().Display();            
		            }
		            else{
		                getValidationInstance()._RemoveMessage(this._customizeValidation[i].MessageID, this._customizeValidation[i].UniqueID);
			            getValidationInstance().Display();
		            }
		            result = result && _result;
		        }catch{ _result = true;}
		    }
		}
		
		if(result > 0){
			return true;
		}
		else{
			return false;
		}
	}
	
	this.Display = function(){
	    for(var i=0;i<this.MessageHelper.length;i++){
	        this.MessageHelper[i].Display();
	    }	
	}
	
	this._AddMessage = function(messageID, msgObj){
	    for(var i=0;i< this.MessageHelper.length;i++){
	        if(this.MessageHelper[i].MessageContainer == messageID){
	            this.MessageHelper[i].AddMessage(msgObj);
	            return;
	        }	    
	    }
	    
	    var _Message = new MessageShow();
	    _Message.MessageContainer = messageID;
	    _Message.AddMessage(msgObj);
	    this.MessageHelper.push(_Message);
	}
	
	this._RemoveMessage = function(messageID, ctrlID){
	    for(var i=0;i< this.MessageHelper.length;i++){
	        if(this.MessageHelper[i].MessageContainer == messageID){
	            this.MessageHelper[i].RemoveMessage(ctrlID);   
	        }
	    }
	}

	this.AddCustomizeValidation = function(cv){
	    try{
	       if(cv != null && typeof(cv) != "undefined"){
	            cv.UniqueID = GetUnqiueID();
	            this._customizeValidation.push(cv);   
	       }        
        }
        catch{}
	}
}

var _c_web_validation = null;
function getValidationInstance(){
	return _c_web_validation;
}
function setValidationInstance(obj){
	_c_web_validation = obj;
}

var _c_Validation_Unqiue_ID = [];
function GetUnqiueID(){
    try{
        var maxID = _c_Validation_Unqiue_ID.length;
        var newID = "cv_UnqiueID" + (maxID + 1).toString();
        _c_Validation_Unqiue_ID.push(newID);
        return newID;
    }
    catch
    {
       _c_Validation_Unqiue_ID = [];
       return GetUnqiueID();
    }
}

function ControlMessage(ctrl, message){
	this.Control = null;
	if(typeof(ctrl) == "string")
		this.Control = document.getElementById(ctrl);
	else if(typeof(ctrl) == "object")
		this.Control = ctrl;
	this.Message = message;
	this.Type = "Message";
	this.Index = 0;
}

ControlMessage.prototype.ShowAll = function(msgContainer,_class){
	var obj = document.getElementById(msgContainer);
	if(obj != null && obj != "undefine"){
		var oli = document.createElement("li");
		oli.innerHTML = this.Message;
		oli.classname = _class;
		obj.appendChild(oli);
	}
}

function MessageShow(){
	this.Style = new MessageStyle();
	this.Messages = new Array();
	this.MessageContainer = "";
	this.Count = function(){
		return this.Messages.length;
	}
	this.CustomizeShow;
}

MessageShow.prototype.Display = function(){
	this.Clear();
	for(var i=0;i<this.Count();i++)
		if(typeof(this.CustomizeShow)=="function") this.CustomizeShow(this.Messages[i]);
		else{this.Messages[i].ShowAll(this.MessageContainer,this.Style.Message);}
}

MessageShow.prototype.Clear = function(){
	document.getElementById(this.MessageContainer).innerHTML = "";
}

//just allow Message Object
MessageShow.prototype.AddMessage = function(msgObj){
	if(typeof(msgObj) == "object" && msgObj.Type == "Message"){
		for(var i=0;i<this.Count();i++){
			if(this.Messages[i].Control != null && this.Messages[i].Control == msgObj.Control){
				this.Messages[i].Message = msgObj.Message;
				return true;	
			}
		}		
		this.Messages.push(msgObj);
		return true;
	}else return false;
}

MessageShow.prototype.RemoveMessage = function(ctrl){
	for(var i=0;i<this.Count();i++){
		if(this.Messages[i].Control != null && this.Messages[i].Control == ctrl){
			this.Messages.splice(i);
			return true;
		}
	}
}

function MessageStyle(){
	this.Main = "";
	this.Title = "";
	this.Message = "";
}

//Client Customize Validation
function CustomizeValidation(){
    this.UniqueID = "";
    this.MessageID = "";
    this.Message = "";
    this.Validate = function(r){
        return true;    
    }
}